package io.github.source.media.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.github.catvod.utils.Trans;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.github.source.Init;

public class Filter implements Parcelable {

    @SerializedName("key")
    private String key;
    @SerializedName("name")
    private String name;
    @SerializedName("init")
    private String init;
    @SerializedName("value")
    private List<Value> value;

    @Override
    public String toString() {
        return "Filter{" +
                "key='" + key + '\'' +
                ", name='" + name + '\'' +
                ", init='" + init + '\'' +
                ", value=" + value +
                '}';
    }

    public static Filter objectFrom(JsonElement element) {
        return Init.json().fromJson(element, Filter.class);
    }

    public static List<Filter> arrayFrom(String result) {
        Type listType = new TypeToken<List<Filter>>() {}.getType();
        List<Filter> items = Init.json().fromJson(result, listType);
        return items == null ? Collections.emptyList() : items;
    }

    public Filter() {
    }

    public String getKey() {
        return key;
    }

    public String getName() {
        return TextUtils.isEmpty(name) ? "" : name;
    }

    public String getInit() {
        return init;
    }

    public List<Value> getValue() {
        return value == null ? Collections.emptyList() : value;
    }

    public String setActivated(String v) {
        int index = getValue().indexOf(new Value(v));
        if (index != -1) getValue().get(index).setActivated(true);
        return v;
    }

    public Filter check() {
        Iterables.removeIf(getValue(), Predicates.isNull());
        return this;
    }

    public Filter trans() {
        if (Trans.pass()) return this;
        for (Value value : getValue()) value.trans();
        return this;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.key);
        dest.writeString(this.name);
        dest.writeString(this.init);
        dest.writeList(this.value);
    }

    protected Filter(Parcel in) {
        this.key = in.readString();
        this.name = in.readString();
        this.init = in.readString();
        this.value = new ArrayList<>();
        in.readList(this.value, Value.class.getClassLoader());
    }

    public static final Creator<Filter> CREATOR = new Creator<Filter>() {
        @Override
        public Filter createFromParcel(Parcel source) {
            return new Filter(source);
        }

        @Override
        public Filter[] newArray(int size) {
            return new Filter[size];
        }
    };


    /**
     * 健-值
     * n-v
     * */
    public static class Value implements Parcelable {

        @SerializedName("n")
        private String n;
        @SerializedName("v")
        private String v;

        private boolean activated;

        @Override
        public String toString() {
            return "Value{" +
                    "n='" + n + '\'' +
                    ", v='" + v + '\'' +
                    ", activated=" + activated +
                    '}';
        }

        public Value() {
        }

        public Value(String v) {
            this.v = v;
        }

        public Value(String n, String v) {
            this.n = Trans.s2t(n);
            this.v = v;
        }

        public String getN() {
            return TextUtils.isEmpty(n) ? "" : n;
        }

        public String getV() {
            return TextUtils.isEmpty(v) ? "" : v;
        }

        public void setV(String v) {
            this.v = v;
        }

        public boolean isActivated() {
            return activated;
        }

        public void setActivated(boolean activated) {
            this.activated = activated;
        }

        public void setActivated(Value item) {
            boolean equal = item.equals(this);
            if (activated && equal) activated = false;
            else activated = equal;
        }

        public void trans() {
            this.n = Trans.s2t(n);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) return true;
            if (!(obj instanceof Value)) return false;
            Value it = (Value) obj;
            return getV().equals(it.getV());
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.n);
            dest.writeString(this.v);
            dest.writeByte(this.activated ? (byte) 1 : (byte) 0);
        }

        protected Value(Parcel in) {
            this.n = in.readString();
            this.v = in.readString();
            this.activated = in.readByte() != 0;
        }

        public static final Creator<Value> CREATOR = new Creator<Value>() {
            @Override
            public Value createFromParcel(Parcel source) {
                return new Value(source);
            }

            @Override
            public Value[] newArray(int size) {
                return new Value[size];
            }
        };
    }
}
